package com.jx.service.content;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jx.dao.content.ArticleContentDao;
import com.jx.pojo.entity.content.ArticleContent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 文章内容
 */
@Service
@Slf4j
public class ArticleContentService extends ServiceImpl<ArticleContentDao, ArticleContent> {

    @Resource
    private ArticleContentDao articleContentDao;

    @Transactional
    public void saveArticleContent(ArticleContent articleContent) {
        QueryWrapper<ArticleContent> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", articleContent.getArticleId());
        ArticleContent old = articleContentDao.selectOne(wrapper);
        if (old != null) {
            articleContent.setId(old.getId());
        }
        articleContentDao.insertOrUpdate(articleContent);
    }

    public ArticleContent getArticleContentByArticleId(Integer articleId) {
        QueryWrapper<ArticleContent> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", articleId);
        return articleContentDao.selectOne(wrapper);
    }

    public void deleteByArticleId(Integer articleId) {
        QueryWrapper<ArticleContent> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", articleId);
        articleContentDao.delete(wrapper);
    }

}
